package cn.smartaccount.demo.common.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author:xuluquan
 * @date: 2018/5/29
 * @version:v1
 */
public class DateTimeUtil {

    /**
     * java.util.Date->java.time.LocalDate
     *
     * @author:xuluquan
     * @date :2018/2/11 下午5:54
     * @version:v1
     * @return:
     * @param:
     */
    public static LocalDate dateToLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone).toLocalDate();
    }

    /**
     * java.util.Date->java.time.LocalDateTime
     *
     * @author:xuluquan
     * @date :2018/2/11 下午5:55
     * @version:v1
     * @return:
     * @param:
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    /**
     * java.time.LocalDateTime --> java.util.Date
     *
     * @author:xuluquan
     * @date :2018/2/11 下午5:58
     * @version:v1
     * @return:
     * @param:
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return Date.from(instant);
    }

    /**
     * java.time.LocalDate --> java.util.Date
     *
     * @author:xuluquan
     * @date :2018/2/11 下午6:00
     * @version:v1
     * @return:
     * @param:
     */
    public static Date localDateToDate(LocalDate localDate) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
        return Date.from(instant);
    }

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public void setDateTimeFormatter(String date) {
        LocalDateTime dateTime = LocalDateTime.parse(date, DATE_TIME_FORMATTER);
    }

    private static DecimalFormat df;

    /**
     * 金额转为千分位，保留两位小数
     *
     * @return 格式化的字符串
     * @author xuluquan
     * @date 2018-10-19 11:14
     **/
    public static String format(BigDecimal bigDecimal) {

        if (df != null) {
            return df.format(bigDecimal);
        }
        synchronized (DateTimeUtil.class) {
            if (df != null) {
                return df.format(bigDecimal);
            }
            df = new DecimalFormat("#,##0.00");
        }
        return df.format(bigDecimal);
    }

    /**
    *  获取当前时间
    *
    * @author xuluquan
    * @date   2019-06-09 16:44
    * @param
    * @return long
    */
    public static long getTime() {
        Instant now = Instant.now().atOffset(ZoneOffset.of("+8")).toInstant();
        return now.toEpochMilli();
    }

    public static void main(String[] args) {

     /*   LocalDate localDate=dateToLocalDate(new Date());
        LocalDate localDate2=dateToLocalDate(new Date()).plusDays(-1);
        long day=localDate.until(localDate2, ChronoUnit.DAYS);
        System.out.println(day);*/
        System.out.println(LocalDate.now().atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli());

    }

}
